package ywpt.web.common;

import java.io.IOException;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * String Utility Class This is used to encode passwords programmatically
 * 
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible </a>
 * @version $Revision: 1.1 $ $Date: 2008/05/06 07:28:15 $
 */
public class StringUtil {
	// ~ Static fields/initializers
	// =============================================

	/** The <code>Log</code> instance for this class. */
	private static Log log = LogFactory.getLog(StringUtil.class);

	// ~ Methods
	// ================================================================

	/**
	 * Encode a string using algorithm specified in web.xml and return the
	 * resulting encrypted password. If exception, the plain credentials string
	 * is returned
	 * 
	 * @param password
	 *            Password or other credentials to use in authenticating this
	 *            username
	 * @param algorithm
	 *            Algorithm used to do the digest
	 * 
	 * @return encypted password based on the algorithm.
	 */
	public static String encodePassword(String password, String algorithm) {
		byte[] unencodedPassword = password.getBytes();

		MessageDigest md = null;

		try {
			// first create an instance, given the provider
			md = MessageDigest.getInstance(algorithm);
		} catch (Exception e) {
			log.error("Exception: " + e);

			return password;
		}

		md.reset();

		// call the update method one or more times
		// (useful when you don't know the size of your data, eg. stream)
		md.update(unencodedPassword);

		// now calculate the hash
		byte[] encodedPassword = md.digest();

		StringBuffer buf = new StringBuffer();

		for (byte element : encodedPassword) {
			if ((element & 0xff) < 0x10) {
				buf.append("0");
			}

			buf.append(Long.toString(element & 0xff, 16));
		}

		return buf.toString();
	}

	/**
	 * Encode a string using Base64 encoding. Used when storing passwords as
	 * cookies.
	 * 
	 * This is weak encoding in that anyone can use the decodeString routine to
	 * reverse the encoding.
	 * 
	 * @param str
	 * @return String
	 * @throws IOException
	 */
	public static String encodeString(String str) throws IOException {
		sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder();
		String encodedStr = new String(encoder.encodeBuffer(str.getBytes()));

		return (encodedStr.trim());
	}

	/**
	 * Decode a string using Base64 encoding.
	 * 
	 * @param str
	 * @return String
	 * @throws IOException
	 */
	public static String decodeString(String str) throws IOException {
		sun.misc.BASE64Decoder dec = new sun.misc.BASE64Decoder();
		String value = new String(dec.decodeBuffer(str));

		return (value);
	}

	public static Date StrToDate(String a_sDate) throws Exception {
		Date ret = null;
		try {
			if ((a_sDate == null) || a_sDate.equals("")) {
				throw new Exception(
						"CommonUtil:StrToDate() \u4E0D\u80FD\u4E3A\u7A7A!");
			}
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			ret = dateFormat.parse(a_sDate);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("StringUtil:StrToDate() Error !" + a_sDate
					+ e.getMessage());
		}
		return ret;
	}

	public static String replaceSubstr(String s, String s1, String s2) {
		StringBuffer stringbuffer = new StringBuffer();
		int i = s1.length();
		for (int j = 0; (j = s.indexOf(s1)) != -1;) {
			stringbuffer.append(s.substring(0, j) + s2);
			s = s.substring(j + i);
		}

		stringbuffer.append(s);
		return stringbuffer.toString();
	}

}
